<?php
namespace app\seller\controller;

class StoreInfo extends \app\common\controller\Seller
{

    protected $_state = [
        '审核中',
        '已审核',
        '平台自营店'
    ];

    /**
     * 店铺信息
     */
    public function index()
    {
        $member_id = $this->member_id;
        $store_info = rpc('common/common/one', [
            'store_joinin',
            [
                'member_id' => $member_id
            ]
        ]);
        if (! empty($store_info)) {
            $this->view->assign('store_info', $store_info);
        }
        if(!empty($store_info['store_class_names']) && !empty($store_info['store_class_commis_rates'])){
            $store_info['store_class_names'] = unserialize($store_info['store_class_names']);
            $rate_arr = explode(',', ($store_info['store_class_commis_rates']));
            if(is_array($store_info['store_class_names'])){
            	foreach($store_info['store_class_names'] as $key => $val){
            	    $tmp = explode(',', $val);
            		$goods_class[] = ['one_name'=>$tmp[0],'two_name'=>$tmp[1],'commis_rate'=>$rate_arr[0]?$rate_arr[0]:0];
            	}
            	$this->assign('goods_class',$goods_class);
            }
        }
        $this->assign('left_nav', 'bind_class');
        return $this->view->fetch('store/store_info');
    }

    /**
     * 经营类目
     *
     * @return html
     */
    public function bind_class()
    {
        $bindClass = rpc('common/common/lists', [
            'store_bind_class',
            [
                'store_id' => $this->store_id
            ]
        ]);
        $goodsClass = \Fields::getById('goods_class');
        $this->assign('left_nav', 'bind_class');
        return view('store/bind_class', [
            'bind_class' => $bindClass,
            'goods_class' => $goodsClass,
            'state' => $this->_state
        ]);
    }

    /**
     * 申请经营类目
     *
     * @return html
     */
    public function bind_class_add()
    {
        if (request()->isPost()) {
            $goodsClass = request()->post('goods_class');
            if (preg_match('/^[\d,]+$/', $goodsClass)) {
                list ($class_1, $class_2) = explode(',', trim($goodsClass, ','));
            } else {
                showDialog('申请失败');
            }
            $data['store_id'] = $this->store_id;
            $data['class_1'] = intval($class_1);
            $data['class_2'] = intval($class_2);
            $goodClass = \Fields::getById('goods_class', $data['class_2']);
            if (isset($goodClass['commis_rate']) && ! empty($goodClass['commis_rate'])) {
                $data['commis_rate'] = $goodClass['commis_rate'];
            } else {
                $data['commis_rate'] = 0;
            }
            $data['state'] = 0;
            $result = rpc('seller/store/bind_class_add', [
                $data
            ]);
            if ($result['result'] >= 0) {
                showDialog('申请成功，请等待系统审核！', 'reload', 'succ');
            } elseif ($result['result'] < 0) {
                showDialog($result['msg'], 'reload');
            } else {
                showDialog('申请失败', 'reload');
            }
        } else {
            $goodsClass = \Fields::getById('goods_class');
            $this->assign('left_nav', 'bind_class');
            return view('store/bind_class_add', [
                'goods_class' => $goodsClass
            ]);
        }
    }

    /**
     * 删除经营类目
     *
     * @return html
     */
    public function bind_class_del()
    {
        $id = request()->get('id/d');
        if (! $id) {
            showDialog('无相关信息');
        }
        $result = rpc('seller/store/delete', [
            'store_bind_class',
            [
                'id' => $id
            ]
        ]);
        if ($result['result']) {
            showDialog('已删除', 'reload', 'succ');
        } else {
            showDialog('删除失败', 'reload');
        }
    }

    public function json_class()
    {
        $id = request()->get('gc_id/d');
        $goodsClass = config('mall_cate');
        if (! isset($goodsClass[$id])) {
            exit('');
        }
        if (isset($goodsClass[$id]) && is_array($goodsClass[$id]) && isset($goodsClass[$id]['_child']) && is_array($goodsClass[$id]['_child'])) {
            $goodsClass = $goodsClass[$id]['_child'];
        }
        $array = [];
        if (is_array($goodsClass) && count($goodsClass) > 0) {
            foreach ($goodsClass as $val) {
                $array[$val['id']] = [
                    'gc_id' => $val['id'],
                    'gc_name' => htmlspecialchars($val['name']),
                    'gc_parent_id' => $val['pid'],
                    'commis_rate' => $val['commis_rate'],
                    'gc_sort' => $val['sort']
                ];
            }
        }
        /**
         * 转码
         */
        $array = array_values($array);
        echo request()->get('callback') . '(' . json_encode($array) . ')';
    }

    /**
     * 续签
     */
    public function reopen()
    {
        // to do 续签
        return '';
        // return view('store/reopen');
    }
}
